package com.jair.common.Inteceptor;

import com.jair.common.constant.UserConstant;
import com.jair.common.constant.bizz.ErrorCode;
import com.jair.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class AuthInteceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        log.info("请求开始，路径：{}", request.getRequestURI());
        // 1.获取请求路径
        //拿到 session，这里自动根据请求头里面的sessionId拿到session
        Object attribute = request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
        if (attribute == null){
            log.error("用户未登录");
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        log.info("用户已登录，用户信息：{}", attribute);
        return true;
    }

    }
